﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) GHI Electronics, LLC.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/* Copyright 2014 Pasi J. Elo, blackdogspark@hotmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

using GHI.Glide;
using GHI.Glide.Display;
using Microsoft.SPOT;

namespace PJE.GlideEx.UI
{
	/// <summary>
	/// The AnimatedImage component allows displaying animated images.
	/// </summary>
	public sealed class AnimatedImage : DisplayObject
	{
		private int _frames;
		private int _index;
		private Bitmap _bitmap;
		private bool _pressed;

		/// <summary>
		/// Holde the bitmap saquence to animate.
		/// </summary>
		public Bitmap Bitmap
		{
			get
			{
				return _bitmap;
			}
			set
			{
				_bitmap = value;
				_frames = _bitmap.Width / Width;
			}
		}

		/// <summary>
		/// Creates a new Animated Image.
		/// </summary>
		/// <param name="name">Name</param>
		/// <param name="alpha">Alpha</param>
		/// <param name="x">X-axis position.</param>
		/// <param name="y">Y-axis position.</param>
		/// <param name="width">Width of one frame</param>
		/// <param name="height">Height of one frame</param>
		public AnimatedImage(string name, ushort alpha, int x, int y, int height, int width)
		{
			Name = name;
			Alpha = alpha;
			X = x;
			Y = y;
			Height = height;
			Width = width;

			// Default
			_bitmap = new Bitmap(Width, Height);
			_bitmap.DrawRectangle(0, 0, 0, 0, Width, Height, 0, 0, Colors.Fuchsia, 0, 0, Colors.Fuchsia, 0, 0, Alpha);
		}

		/// <summary>
		/// Play next frame.
		/// </summary>
		public void NextFrame()
		{
			if (_index < _frames - 1)
				_index++;
			else
				_index = 0;
		}

		/// <summary>
		/// Reset animmation.
		/// </summary>
		public void Reset()
		{
			_index = 0;
		}

		/// <summary>
		/// Disposes all disposable objects in this object.
		/// </summary>
		public override void Dispose()
		{
			_bitmap.Dispose();
			base.Dispose();
		}

		/// <summary>
		/// Renders the image frame onto it's parent container's graphics.
		/// </summary>
		public override void Render()
		{
			var y = Y + Parent.Y;
			var x = X + Parent.X;
			Parent.Graphics.DrawImage(x, y, _bitmap, Width * _index, 0, Width, Height, Alpha);
			base.Render();
		}

		/// <summary>
		/// Handles the touch down event.
		/// </summary>
		/// <param name="e">Touch event arguments.</param>
		/// <returns>Touch event arguments.</returns>
		public override TouchEventArgs OnTouchDown(TouchEventArgs e)
		{
			if (!Rect.Contains(e.Point)) return e;
			_pressed = true;
			e.StopPropagation();
			return e;
		}

		/// <summary>
		/// Handles the touch up event.
		/// </summary>
		/// <param name="e">Touch event arguments.</param>
		/// <returns>Touch event arguments.</returns>
		public override TouchEventArgs OnTouchUp(TouchEventArgs e)
		{
			if (Rect.Contains(e.Point))
			{
				if (!_pressed) return e;
				_pressed = false;
				TriggerTapEvent(this);
				e.StopPropagation();
			}
			else
			{
				if (_pressed)
					_pressed = false;
			}
			return e;
		}
	}
}
